package com.doublecheck.project.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.editor.client.Editor.Path;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.chart.client.chart.Chart;
import com.sencha.gxt.chart.client.chart.axis.GaugeAxis;
import com.sencha.gxt.chart.client.chart.series.GaugeSeries;
import com.sencha.gxt.chart.client.draw.Color;
import com.sencha.gxt.chart.client.draw.RGB;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.fx.client.easing.Default;
import com.sencha.gxt.fx.client.easing.EasingFunction;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

public class GaugeChart extends Chart<Item> {
	/*
	 * Metadata for Line Chart
	 */
	private ArrayList<Item> data;
	private String title;
	private Window window;
	private ContentPanel panel;
	private VerticalLayoutContainer layout;

	public GaugeChart(GaugeChart_Dataset dataset, String title) {
		this.data = dataset.getDataset();
		this.title = title;
	}
	
	public GaugeChart(){}
	/*
	 * Access to Data Object
	 */
	public interface DataPropertyAccess extends PropertyAccess<Item> {
		ValueProvider<Item, Double> value0();
	    ValueProvider<Item, String> category();
	    @Path("category")
	    ModelKeyProvider<Item> categoryKey();
	}

	private static final DataPropertyAccess dataAccess = GWT.create(DataPropertyAccess.class);
	@Override
	  public Widget asWidget() {
	    final ListStore<Item> store = new ListStore<Item>(dataAccess.categoryKey());
	    store.addAll(data);	 
	    final Chart<Item> chart = createGauge(store, 0, new RGB("#F49D10"), true, new Default(), dataAccess.value0());
	 
	    panel = new FramedPanel();
	    panel.getElement().getStyle().setMargin(10, Unit.PX);
	    panel.setCollapsible(true);
	    panel.setHeadingText(title);
	    panel.setPixelSize(400, 320);
	    panel.setBodyBorder(true);
	 
	    layout = new VerticalLayoutContainer();
	    chart.setLayoutData(new VerticalLayoutData(1, 1));	
	    layout = new VerticalLayoutContainer();
	    layout.add(chart);
	    layout.setLayoutData(new VerticalLayoutData(1, 1));	    
	    initiateWindow();	
	 
	    return panel;
	  }
	/*For zoom-in event of the chart*/
	public void initiateWindow(){
		final VerticalLayoutContainer panelLayout = new VerticalLayoutContainer();
		final ToolBar toolBar = new ToolBar();		
		window = new Window();
	    window.setPixelSize(900, 700);
	    window.setModal(true);
	    window.setBlinkModal(true);	    
	    window.setHeadingText("Line Chart");
	    window.addHideHandler(new HideHandler() {
	        @Override
	        public void onHide(HideEvent event) {
	            TextButton open = ((Window) event.getSource()).getData("open");
	            open.focus();
	            panelLayout.clear();
	            panelLayout.add(toolBar);
	            panelLayout.add(layout);
				panel.add( panelLayout);
	        }
	    });
	    /*Close button at the bottom of window*/
		TextButton closeBtn = new TextButton("Close");
		closeBtn.addSelectHandler(new SelectHandler() {
			@Override
			public void onSelect(SelectEvent event) {
				panelLayout.clear();
		        panelLayout.add(toolBar);
		        panelLayout.add(layout);
				panel.add(panelLayout);
				window.hide();
			}
		});
		window.addButton(closeBtn);
		window.setFocusWidget(window.getButtonBar().getWidget(0));
		/*zoom-in button on toolbar*/	    
	    TextButton openBtn = new TextButton("Zoom In");
	    openBtn.addSelectHandler(new SelectHandler() {
	    	@Override
	    	public void onSelect(SelectEvent event) {
	    		window.add(layout);
	    		window.show();
	    	}
	    });
	    window.setData("open", openBtn);	    	    
	    toolBar.add(openBtn);
	    toolBar.setLayoutData(new VerticalLayoutData(1, -1));
	    	    
        panelLayout.add(toolBar);
        panelLayout.add(layout);	
		panel.add(panelLayout);
	}	 

	 
	  private Chart<Item> createGauge(ListStore<Item> store, double donut, Color color, boolean needle,
	      EasingFunction easing, ValueProvider<Item, Double> provider) {
	    Chart<Item> chart = new Chart<Item>();
	    chart.setStore(store);
	    chart.setAnimationDuration(750);
	    chart.setAnimationEasing(easing);
	    chart.setDefaultInsets(20);
	 
	    GaugeAxis<Item> axis = new GaugeAxis<Item>();
	    axis.setMargin(8);
	    axis.setDisplayGrid(true);
	    axis.setMinimum(0);
	    axis.setMaximum(100);
	    chart.addAxis(axis);
	 
	    final GaugeSeries<Item> gauge = new GaugeSeries<Item>();
	    gauge.addColor(color);
	    gauge.addColor(new RGB("#ddd"));
	    gauge.setAngleField(provider);
	    gauge.setNeedle(needle);
	    gauge.setDonut(donut);
	    chart.addSeries(gauge);
	 
	    return chart;
	  }

}

